package icu.lookyousmileface.nio.zero;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @author shenj
 * @title: NioClient
 * @projectName NettyPro
 * @date 2020/12/2 14:38
 */
public class NioClient implements Serializable {

    private static final  String fileName = "E:\\百度网盘迁移\\尚硅谷Netty学习资料\\资料\\protoc-3.6.1-win32.zip";
    private static final String  host = "127.0.0.1";
    private static final int port = 8899;

    public static void main(String[] args) throws IOException {

        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress(host,port));
        //文件传输通道
        FileChannel fileChannel = new FileInputStream(new File(fileName)).getChannel();

        long start = System.currentTimeMillis();

        long byteNum = fileChannel.transferTo(0, fileChannel.size(), socketChannel);

        System.out.println("传输的字节数："+byteNum+"耗时："+(System.currentTimeMillis()-start));

        fileChannel.close();

    }
}
